/**
 * 
 */
package com.toncen.samepms.fisher.formatter;

import com.toncen.samepms.basic.dao.OrganizationDao;
import com.toncen.samepms.basic.domain.Organization;
import com.toncen.samepms.system.dao.CodeDao;
import com.toncen.samepms.system.domain.Code;
import com.toncen.samepms.system.domain.CodeItem;
import com.sky.common.string.StringUtils;
import com.sky.fisher.formatter.AbstractFormatter;
import com.sky.fisher.model.CommonColumnModel;

/**
 * 描述：组织类型格式化器
 * 
 * @author tch
 * 
 */
public class OrganizationTypeFormatter extends AbstractFormatter {
	/**
	 * 
	 */
	public OrganizationTypeFormatter() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sky.fisher.formatter.AbstractFormatter#format(com.sky.fisher.model.CommonColumnModel,
	 *      java.lang.String)
	 */
	@Override
	public String format(CommonColumnModel column, String source) throws Exception {
		if (!StringUtils.isEmpty(source)) {
			CodeDao codeDao = new CodeDao();
			String code = "organization_type";
			Code codeBean = codeDao.getByCode(code);
			if (codeBean == null) {
				throw new Exception("编码【" + code + "】不存在，请与系统管理员联系！");
			}
			OrganizationDao<Organization> organizationDao = new OrganizationDao<Organization>();
			Organization organization = organizationDao.load(Long.parseLong(source));
			StringBuffer result = new StringBuffer();
			String discription;
			for (String type : organization.getProxyTypes()) {
				for (CodeItem codeItem : codeBean.getCodeItemList()) {
					if (type.equals(codeItem.getValue())) {
						discription = codeItem.getName();
						if (!StringUtils.isEmpty(codeItem.getExtend1())//
								&& StringUtils.isEmpty(column.getParameterMap().get("edittype"))//
								&& StringUtils.isEmpty(column.getParameterMap().get("editname"))) {//
							discription = "<span style='color:" + codeItem.getExtend1() + "'>" + codeItem.getName() + "</span>";
						}
						result.append(",");
						result.append(discription);
					}
				}
			}
			return result.toString().replaceFirst(",", "");
		}
		return "";
	}

}
